package com.member.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.admin.domain.AdminCommand;
import com.board.util.PagingUtil;
import com.member.dao.MemberDao;

@Controller
public class SendSmsController {
	private int pageSize = 10;
	private int blockCount = 10;
	
	@Autowired
	private MemberDao memberDao;
	
	@RequestMapping("/member/sendSmsList.do")
	public ModelAndView process(@RequestParam(value="pageNum",defaultValue="1")int currentPage,
								@RequestParam(value="keyField",defaultValue="")String keyField,
								@RequestParam(value="keyWord",defaultValue="")String keyWord,
								HttpServletRequest request){
		String pagingHtml = "";
		HttpSession session = request.getSession();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("keyField", keyField);
		map.put("keyWord", keyWord);
		map.put("id", session.getAttribute("id"));
		
		int count = memberDao.getSendSmsCount(map);
		
		PagingUtil page = new PagingUtil(keyField, keyWord, currentPage, count, blockCount,pageSize,"sendSmsList.do");
		
		pagingHtml=page.getPagingHtml().toString();
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		System.out.println(session.getAttribute("id"));
		List<AdminCommand> list = null;
		if(count >0){
			list = memberDao.sendSmsList(map);
		}else {
			list = Collections.emptyList();
		}
		
		int number = count-(currentPage-1)*pageSize;
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("memberSendSmsList");
		mav.addObject("count", count);
		mav.addObject("list", list);
		mav.addObject("pagingHtml", pagingHtml);
		mav.addObject("number", number);
		mav.addObject("currentPage",currentPage);
		return mav;
		
	}
}
